package mdm.led.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mdm.led.dao.UserMapper;
import mdm.led.entity.ExternalUser;
import mdm.led.entity.User;
import mdm.led.service.ExternalUserService;
import mdm.led.util.MD5;

/**
 *
 * @author :Monarch丿He
 * @date :2018年12月11日
 * @explain :外部联系人业务实现
 */
@Service("externalUserService")
public class ExternalUserServiceImpl implements ExternalUserService {

	@Autowired
	private UserMapper userDao;

	@Override
	public Integer addExternalUser(ExternalUser user) {
		// 外部联系人负责人Id
		Integer dutyid = user.getDutyid();
		// 负责人用户信息
		User dutyUser = userDao.selectById(dutyid);
		// 当前时间
		Date date = new Date();
		SimpleDateFormat siFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 外部联系人
		User externalUser = new User();
		externalUser.setcOperatorId(dutyid);
		externalUser.setUsertype(2);
		externalUser.setCreateTime(siFormat.format(date));
		short state = 1;
		externalUser.setState(state);
		short states = 1;
		externalUser.setStatus(states);
		externalUser.setDepartmentId(dutyUser.getDepartmentId());
		externalUser.setDepartmentType(dutyUser.getDepartmentType());
		externalUser.setDepartmentLevel(dutyUser.getDepartmentLevel());
		externalUser.setcUsername(user.getAccount());
		// 密码加密
		String standardMd5 = MD5.getStandardMd5(user.getPassword());
		externalUser.setcPassword(standardMd5);
		externalUser.setcNickname(user.getUsername());
		externalUser.setcPhone(user.getPhone());
		externalUser.setcJobnumber("外部联系人");
		externalUser.setRoleId(5);
		int insertSelective = userDao.insertSelective(externalUser);
		return insertSelective;
	}

	@Override
	public Integer updateExternalUser(ExternalUser user) {
		User user2 = new User();
		user2.setcUserId(user.getUserid());
		user2.setcUsername(user.getAccount());
		String standardMd5 = "";
		if(!"".equals(user.getPassword()) && user.getPassword() != null){
			// 密码加密
			standardMd5 = MD5.getStandardMd5(user.getPassword());
		}
		user2.setcPassword(standardMd5);
		user2.setcNickname(user.getUsername());
		user2.setcPhone(user.getPhone());
		user2.setUpdateTime(user.getCreatetime());
		user2.setcOperatorId(user.getDutyid());
		user2.setState(user.getState().shortValue());
		int updateByPrimaryKeySelective = userDao.updateByPrimaryKeySelective(user2);
		return updateByPrimaryKeySelective;
	}

	@Override
	public List<ExternalUser> findExternaUser(ExternalUser user) {
		// 姓名
		String username = user.getUsername();
		// 联系方式
		String phone = user.getPhone();
		// 责任人姓名
		String dutyname = user.getDutyname();
		User user2 = new User();
		user2.setcNickname(username);
		user2.setcOperatorUser(dutyname);
		user2.setcPhone(phone);
		user2.setUsertype(2);
		// 外部联系人集合
		// 用户集合
		List<User> selectByCondition = userDao.selectByCondition(user2);
		// 外部联系人集合
		List<ExternalUser> list = new ArrayList<>();
		ExternalUser externalUser = null;
		// 循环负责外部联系人
		for (User item : selectByCondition) {
			// 实例化对象
			externalUser = new ExternalUser();
			// 赋值
			externalUser.setUserid(item.getcUserId());
			externalUser.setAccount(item.getcUsername());
			externalUser.setUsername(item.getcNickname());
			externalUser.setPassword(item.getcPassword());
			externalUser.setPhone(item.getcPhone());
			externalUser.setCreatetime(item.getCreateTime());
			externalUser.setState(item.getState().intValue());
			externalUser.setDutyid(item.getcOperatorId());
			externalUser.setDutyname(item.getcOperatorUser());
			// 添加到集合
			list.add(externalUser);
		}

		return list;
	}

	@Override
	public ExternalUser findExternaUserById(Integer id) {

		User item = userDao.selectById(id);
		if(item.getUsertype() == 1){
			return null;
		}
		ExternalUser externalUser = new ExternalUser();
		// 赋值
		externalUser.setUserid(item.getcUserId());
		externalUser.setAccount(item.getcUsername());
		externalUser.setUsername(item.getcNickname());
		externalUser.setPassword(item.getcPassword());
		externalUser.setPhone(item.getcPhone());
		externalUser.setCreatetime(item.getCreateTime());
		externalUser.setState(item.getState().intValue());
		externalUser.setDutyid(item.getcOperatorId());
		externalUser.setDutyname(item.getcOperatorUser());

		return externalUser;
	}

	@Override
	public ExternalUser findExternaUserByAccount(String account) {
		User item = userDao.selectByUsername(account);
		if(item == null){
			return null;
		}
		ExternalUser externalUser = new ExternalUser();
		// 赋值
		externalUser.setUserid(item.getcUserId());
		externalUser.setAccount(item.getcUsername());
		externalUser.setUsername(item.getcNickname());
		externalUser.setPassword(item.getcPassword());
		externalUser.setPhone(item.getcPhone());
		externalUser.setCreatetime(item.getCreateTime());
		externalUser.setState(item.getState().intValue());
		externalUser.setDutyid(item.getcOperatorId());
		externalUser.setDutyname(item.getcOperatorUser());

		return externalUser;
	}

}
